<template>
  <div class="searchTop">
    <div class="searchTopNav">
      <div class="back" @click="$router.go(-1)">
        <svg class="icon" aria-hidden="true">
          <use xlink:href="#icon-arrowLeft"></use>
        </svg>
      </div>
      <div class="right">
        <input
          type="text"
          :placeholder="placeholder"
          @keydown.enter="saveKeyWord"
          v-model="searchKeyword"
        />
      </div>
    </div>
    <div class="history" v-show="searchSong.length <= 0">
      <div class="history-left">历史</div>
      <div class="history-right">
        <div
          class="keywordItem"
          v-for="(item, i) in keywordList"
          :key="i"
          @click="goSearch(item)"
        >
          {{ item }}
        </div>
      </div>
    </div>
    <div class="playlist" v-show="searchSong.length > 0">
      <div class="playlist-top" v-if="searchSong.length > 0">
        <div class="left">
          <svg class="icon" aria-hidden="true">
            <use xlink:href="#icon-bofang1"></use>
          </svg>
          <div class="text">
            <div class="title">播放全部</div>
            <div class="number">（共{{ searchSong.length }}首）</div>
          </div>
        </div>
      </div>
      <div class="list">
        <div
          class="playItem"
          v-for="(item, i) in searchSong"
          :key="i"
          @click="setPlay(item, i)"
        >
          <div class="left">
            <div class="index">{{ i + 1 }}</div>
            <div class="content">
              <div class="title">{{ item.name }}</div>
              <div class="author">
                <span
                  class="tag"
                  v-if="searchSong.tags && searchSong.tags.length > 0"
                  >{{ searchSong.tags[0] }}</span
                >
                <span class="name">{{ item.name }}</span>
              </div>
            </div>
          </div>
          <div class="right">
            <svg class="icon" aria-hidden="true">
              <use xlink:href="#icon-bofang2"></use>
            </svg>
            <svg class="icon" aria-hidden="true">
              <use xlink:href="#icon-liebiao-dian"></use>
            </svg>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import * as indexApi from "@/api/index.js";
export default {
  name: "searchTop",
  props: ["playlist"],
  data() {
    return {
      keywordList: [],
      searchKeyword: "",
      placeholder: "周杰伦",
      searchSong: [],
    };
  },
  mounted() {},
  methods: {
    async saveKeyWord() {
      this.keywordList.push(this.searchKeyword);
      //去重
      this.keywordList = Array.from(new Set(this.keywordList));
      if (this.keywordList.length > 10) {
        this.keywordList = this.keywordList.slice(
          this.keywordList.length - 10,
          this.keywordList.length
        );
      }
      localStorage.keywordList = JSON.stringify(this.keywordList);
      //获取音乐
      await this.searchMusic();
    },
    async goSearch(item) {
      if (item != this.searchKeyword) {
        this.searchKeyword = item;
        await this.saveKeyWord();
      }
    },
    async searchMusic() {
      let data = await indexApi.goSearch({
        keywords: this.searchKeyword,
      });
      console.log(data.result);
      this.searchSong = data.result.songs;
    },

    setPlay(item, i) {
      item.al = item.album;
      item.al.picUrl = item.album.artist.img1v1Url;
      this.$store.commit("pushPlayList", item);
      this.$store.commit(
        "setPlayIndex",
        this.$store.state.playlist.tracks.length - 1
      );
    },
  },
  beforeMount() {
    if (localStorage.keywordList)
      this.keywordList = JSON.parse(localStorage.keywordList);
  },
};
</script>
<style lang="less" scoped>
.searchTop {
  width: 7.5rem;
  padding: 0 0.4rem;
  .searchTopNav {
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 1rem;
    font-size: 0.35rem;
    .icon {
      width: 0.6rem;
      height: 0.6rem;
      fill: #000;
    }
    .right {
      padding: 0 0.4rem;
      flex: 1;
      input {
        width: 100%;
        border: none;
        border-bottom: 1px solid #666;
        line-height: 0.6rem;
      }
      input:focus {
        outline: none;
      }
    }
  }
  .history {
    display: flex;
    .history-left {
      width: 1.2rem;
      height: 0.6rem;
      font-weight: 900;
      color: #000;
      line-height: 0.6rem;
      margin: 0.2rem 0;
    }
    .history-right {
      color: #666;
      display: flex;
      flex-wrap: wrap;
      flex: 1;
      .keywordItem {
        background-color: #eee;
        height: 0.6rem;
        line-height: 0.6rem;
        padding: 0 0.2rem;
        border-radius: 0.4rem;
        margin: 0.2rem 0.1rem;
      }
    }
  }
  .playlist {
    background-color: #fff;
    border-top-left-radius: 0.3rem;
    border-top-right-radius: 0.3rem;
    margin-top: 0.4rem;
    .playlist-top {
      display: flex;
      justify-content: space-between;
      height: 0.8rem;
      align-items: center;
      .left {
        display: flex;
        align-items: center;
        .icon {
          width: 0.4rem;
          height: 0.4rem;
        }
        .title {
          font-size: 0.34rem;
          margin-left: 0.3rem;
        }
        .number {
          font-size: 0.24rem;
          color: #666;
        }
        .text {
          display: flex;
          align-items: center;
        }
      }
      .btn {
        font-size: 0.24rem;
        color: #fff;
        background: orangered;
        height: 0.6rem;
        line-height: 0.4rem;
        padding: 0.1rem;
        border-radius: 0.4rem;
      }
    }
    .list {
      .playItem {
        display: flex;
        justify-content: space-between;
        align-items: center;
        height: 1rem;
        margin: 0.2rem;
        .left {
          display: flex;
          align-items: center;
          color: #666;
          flex: 1;
          .index {
            width: 0.6rem;
          }
          .title {
            font-size: 0.3rem;
            color: #000;
            margin-bottom: 0.1rem;
            overflow: hidden;
            display: -webkit-box;
            -webkit-line-clamp: 1;
            -webkit-box-orient: vertical;
          }
          .author {
            display: flex;
            height: 0.4rem;
          }
          .tag {
            font-size: 0.2rem;
            color: orangered;
            border: 1px solid orangered;
            border-radius: 0.1rem;
            margin-right: 0.1rem;
            width: 0.8rem;
            text-align: center;
          }
          .name {
            overflow: hidden;
          }
        }
        .right {
          .icon {
            margin-left: 0.1rem;
            width: 0.5rem;
            height: 0.5rem;
            fill: #666;
          }
        }
      }
    }
  }
}
</style>